package io.renren.modules.seller.controller;

import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.entity.UserEntity;
import io.renren.modules.seller.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 用户表
 *
 * @author hou 15373178086@163.com
 * @date 2020-02-16 11:30:48
 */
@Slf4j
@RestController
@RequestMapping("manage/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 返回全部列表
     *
     * @return TbBrands
     */
    @RequestMapping("/findAll")
    public List<UserEntity> findAll() {
        return userService.list();
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody UserEntity userEntity, String page, String limit) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", limit);
        log.info("brand search params:{}", params);
        return userService.findPage(userEntity, params);
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestParam Map<String, Object> params) {
        log.info("brand findPage params:{}", params);
        return userService.findPage(params);
    }

    /**
     * 增加
     *
     * @param brand brand
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody UserEntity brand) {
        try {
            userService.save(brand);
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 修改
     *
     * @param brand brand
     * @return Result
     */
    @RequestMapping("/update")
    public Result update(@RequestBody UserEntity brand) {
        try {
            userService.updateById(brand);
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 获取实体
     *
     * @param id id
     * @return BrandEntity
     */
    @RequestMapping("/findOne")
    public UserEntity findOne(Long id) {
        return userService.getById(id);
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return Result
     */
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            userService.removeByIds(Arrays.asList(ids));
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(Long id, String status) {
        try {
            UserEntity userEntity = new UserEntity();
            userEntity.setId(id);
            userEntity.setStatus(status);
            userEntity.setUpdated(new Date());
            userService.updateById(userEntity);
            return new Result(true, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("失败");
        }
    }
}
